﻿/// Write a method that return the maximal element in a portion of array of integers starting at given index.
/// Using it write another method that sorts an array in ascending / descending order.


using System;

class Zad9
{
    static void Main()
    {
        int[] arr = { 5, 3, 4, 7, 6, 4, 5 };

        int biggest = BiggestFromIndex(arr, 2);
        Console.WriteLine("Biggest from index {0} is {1}", 2, biggest);

        Console.WriteLine("Sort ascending ");
        SelectionSort(arr);

        Print(arr);
        
        Console.WriteLine();
        Console.WriteLine("Sort descending ");
        SelectionSort(arr, false);
        Print(arr);

        Console.WriteLine();

    }

    private static void Print(int[] arr)
    {
        foreach (var item in arr)
        {
            Console.Write(item + " ");
        }
    }

    private static void SelectionSort(int[] arr, bool asc = true)
    {
        int minIndex;
        int temp;

        for (int i = 0; i < arr.Length - 1; i++)
        {
            minIndex = i;

            for (int j = i + 1; j < arr.Length; j++)
            {
                if (CheckDirection(arr[j], arr[minIndex], asc))
                {
                    minIndex = j;
                }
            }

            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    private static bool CheckDirection(int a, int b, bool asend = true)
    {
        if (asend)
        {
            return a < b;
        }
        else
        {
            return a > b;
        }
    }

    static int BiggestFromIndex(int[] arr, int index)
    {
        int length = arr.Length;
        int biggest = 0;
        for (int i = index; i < length; i++)
        {
            if (arr[i] > biggest)
            {
                biggest = arr[i];
            }
        }
        return biggest;
    }

}